﻿using Api.Shared.Model.Common;
using Api.Shared.Model.Dto;
using Api.Shared.Model.Fafu;
using Api.Shared.Model.XinYa;
using AutoMapper;

namespace Api.Shared.Sugar.Profiles
{
    public class MemberProfile : Profile
    {
        public MemberProfile()
        {
            //源,目标
            CreateMap<Device, DeviceDto>()
                .ForMember(
                    dest => dest.DevicePhoto,
                    opt => opt.MapFrom(src => MapDevicePhoto(src.DevicePhoto))
                )
                .ForMember(
                    dest => dest.DevicePhotoUrl,
                    opt => opt.MapFrom(src => MapDevicePhoto(src.DevicePhotoUrl))
                );

            CreateMap<ParamFengtousMysql, ParamFengtouDto>()
                .ForMember(
                    dest => dest.FengtouPhoto,
                    opt => opt.MapFrom(src => MapDevicePhoto(src.FengtouPhoto))
                )
                .ForMember(
                    dest => dest.FengtouPhotoUrl,
                    opt => opt.MapFrom(src => MapDevicePhoto(src.FengtouPhotoUrl))
                );

            CreateMap<ParamFengtouDto, ParamFengtousMysql>()
                .ForMember(
                    dest => dest.FengtouPhoto,
                    opt => opt.MapFrom(src => string.Join(",", src.FengtouPhoto))
                )
                .ForMember(
                    dest => dest.FengtouPhotoUrl,
                    opt => opt.MapFrom(src => string.Join(",", src.FengtouPhotoUrl))
                );



            // 替换 TestReport 到 TestReportDto 的映射部分，避免在表达式树中声明 out 变量
            CreateMap<TestReport, TestReportDto>()
                .ForMember(
                    dest => dest.VoltageList,
                    opt => opt.MapFrom(src =>
                        string.IsNullOrWhiteSpace(src.VoltageListStr)
                            ? new List<double>()
                            : src.VoltageListStr.Split(',', StringSplitOptions.RemoveEmptyEntries)
                                .Select(ParseDoubleOrDefault)
                                .ToList()
                    )
                )
                .ForMember(
                    dest => dest.CurrentList,
                    opt => opt.MapFrom(src =>
                        string.IsNullOrWhiteSpace(src.CurrentListStr)
                            ? new List<double>()
                            : src.CurrentListStr.Split(',', StringSplitOptions.RemoveEmptyEntries)
                                .Select(ParseDoubleOrDefault)
                                .ToList()
                    )
                )
                .ForMember(
                    dest => dest.PDList,
                    opt => opt.MapFrom(src =>
                        string.IsNullOrWhiteSpace(src.PDListStr)
                            ? new List<double>()
                            : src.PDListStr.Split(',', StringSplitOptions.RemoveEmptyEntries)
                                .Select(ParseDoubleOrDefault)
                                .ToList()
                    )
                );


        }

        // 修改 MapDevicePhoto 方法的参数为 string?，以允许传递 null 值，消除 CS8604 警告。
        private string[]? MapDevicePhoto(string? devicePhoto)
        {
            return devicePhoto?.Split(',');
        }

        // 在 MemberProfile 类中添加如下私有静态方法
        private static double ParseDoubleOrDefault(string s)
        {
            double result;
            return double.TryParse(s, out result) ? result : 0;
        }
    }
}
